package com.gongzhiyun.springboot3test.service.impl;

import com.gongzhiyun.springboot3test.base.R;
import com.gongzhiyun.springboot3test.entity.dialogue.PrivateMessages;
import com.gongzhiyun.springboot3test.entity.user.AuthInfo;
import com.gongzhiyun.springboot3test.entity.user.TbUser;
import com.gongzhiyun.springboot3test.interceptor.AppInterceptor;
import com.gongzhiyun.springboot3test.mapper.dialogue.PrivateMessagesMapper;
import com.gongzhiyun.springboot3test.mapper.user.TbUserMapper;
import com.gongzhiyun.springboot3test.service.PrivateService;
import com.gongzhiyun.springboot3test.service.UserService;
import com.gongzhiyun.springboot3test.vo.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class PrivateServiceImpl implements PrivateService {
    private final PrivateMessagesMapper privateMessagesMapper;
    private final UserService userService;
    private final TbUserMapper tbUserMapper;

    /**
     * 发布私信
     *
     * @param privateMessages
     * @return
     */
    @Override
    public R<String> send(PrivateMessages privateMessages) {
        if (privateMessages == null) {
            return R.fail("参数不能为空");
        }
        //获取认证信息
        AuthInfo authInfo = AppInterceptor.get();
        Integer uid = authInfo.getUid();
        if (uid == null){
            return R.fail("用户未登录");
        }
        privateMessages.setSenderId(uid);
        int i = privateMessagesMapper.insert(privateMessages);
        return i == 1 ? R.ok("发布私信成功") : R.fail("发布私信失败");
    }

    @Override
    public R<String> sendByOther(String username) {
        if (username == null) {
            return R.fail("请输入要私聊的对象");
        }

        TbUser user = userService.queryByName(username);
        if (user == null) {
            return R.fail("用户不存在");
        }

        PrivateMessages privateMessages = new PrivateMessages();

        privateMessages.setReceiverId(user.getUid());
        privateMessages.setContent("你好呀");

        return send(privateMessages);
    }

    /**
     * 查询跟登录用户有关的所有私信用户
     *
     * @return
     */
    @Override
    public R<List<UserInfo>> getuid() {
        //获取认证信息
        AuthInfo authInfo = AppInterceptor.get();
        Integer uid = authInfo.getUid();
        //调用方法
        List<Integer> listUid = privateMessagesMapper.getPrivateUid(uid);

        List<UserInfo> list = new ArrayList<>();
        for (Integer i : listUid) {
            UserInfo userInfo = userService.queryByIdAll(i).viewData();
            userInfo.setExinfo(userService.IsFollow(uid, i));
            userInfo.setLastWord(privateMessagesMapper.getLastWord(uid, i));
            userInfo.setUnreadCount(privateMessagesMapper.getUnreadCount(uid,i));
            list.add(userInfo);
        }

        return R.ok(list);
    }

    /**
     * 获取登录用户跟id的所有聊天内容
     * @param id
     * @return
     */
    @Override
    public R<List<PrivateMessages>> getDialogue(Integer id) {
        //获取认证信息
        AuthInfo authInfo = AppInterceptor.get();
        Integer uid = authInfo.getUid();
        //获取内容
        List<PrivateMessages> list = privateMessagesMapper.getDialogue(uid, id);


        return R.ok(list);
    }

    /**
     * 获取登录用户跟id的未读消息数量
     * @return
     */
    @Override
    public R<Integer> getUnreadCount(Integer id) {
        //获取认证信息
        AuthInfo authInfo=AppInterceptor.get();
        Integer uid=authInfo.getUid();
        if (uid==null){
            return R.fail("用户未登录");
        }
        return R.ok(privateMessagesMapper.getUnreadCount(uid,id));
    }

    /**
     * 将登录用户与id的未读消息标记为已读
     * @param id
     * @return
     */
    @Override
    public R<String> read(Integer id){
        //获取认证信息
        AuthInfo authInfo=AppInterceptor.get();
        Integer uid=authInfo.getUid();
        if (uid==null){
            return R.fail("用户未登录");
        }
        Integer i = privateMessagesMapper.read(uid, id);
        if (i!=1){
            return R.fail("用户标记已读消息失败");
        }
        return R.ok("标记成功");
    }

}
